// pages/newquanyizhuce/newquanyizhuce.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CUSTOMER_NAME: '',
    CUSTOMER_TEL: '',
    IDCARD: '',
    fuwu: false,
    yinsi: false,
    flag: true, //设置节流
    checked: false,
    timer: "", // 定时器名字
    countDownNum: '60',
    verify: "",
    flag: true, //设置节流
    zongnone: '',
    shenfenjiami: '',
  },




  fetch: function () {
    this.setData({

      jiami: app.globalData.jiami,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,

    })


    if (this.data.flag) {
      this.data.flag = false
      if (this.data.CUSTOMER_TEL == '') {
        wx.showToast({
          title: '请填写手机号！',
          icon: 'none',
          duration: 3000
        })
      } else if (this.data.session_key == "" || this.data.jiami == "" || this.data.session_key == null || this.data.jiami == null || this.data.session_key == undefined || this.data.jiami == undefined) {
        wx.showModal({
          title: '温馨提示',
          content: res.data.data,
          success: function (res) {
            if (res.confirm) { //这里是点击了确定以后   
              console.log('用户点击确定')

              wx.switchTab({
                url: '../../pages/newshouye/newshouye',
              })

            } else { //这里是点击了取消以后
              console.log('用户点击取消')
              wx.switchTab({
                url: '../../pages/newshouye/newshouye',
              })

            }
          }
        })
      } else {
        wx.request({
          url: app.globalData.interface+'/app/API_1052_sendMassage',
          method: 'GET',
          data: {
            phone: this.data.CUSTOMER_TEL,
            KEY: 'xianjgjKey9999',
            OPEN_ID: app.globalData.openid, //用户OPEN_ID
            session_key: this.data.session_key,
            OPEN_UUID: this.data.jiami,
            phone_type: ''
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {

            if (res.data.result == 'error') {
              wx.showToast({
                title: res.data.code,
                icon: 'none',
                duration: 3000
              })
            } else {
              wx.showToast({
                title: res.data.code,
                icon: 'none',
                duration: 3000
              })
            }
          }
        })
        var _this = this;
        var countDownNum = _this.data.countDownNum; // 获取倒计时初始值
        var timer = setInterval(function () {
          countDownNum -= 1;
          _this.setData({
            countDownNum: countDownNum
          })
          if (countDownNum <= -1) {
            clearInterval(timer);
            // 取消置顶的setInterval函数将要执行的代码
            _this.setData({
              countDownNum: 60,
            })
          }
        }, 1000)

      }
      setTimeout(() => {
        this.data.flag = true
      }, 5000)


    }
  },





  baoming() {

    
    this.setData({
      jiami: app.globalData.jiami,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
    })
    
    var that = this

    if (that.data.flag) {
      that.data.flag = false

      if (that.data.checked == false) {
        wx.showToast({
          title: "请勾选用户协议",
          icon: "none",
          duration: 2000
        })

      } else if (that.data.verify == '' || that.data.CUSTOMER_NAME == '' || that.data.CUSTOMER_TEL == '' || that.data.IDCARD == '') {
      
      console.log(that.data.verify);
      console.log(that.data.CUSTOMER_NAME);
      console.log(that.data.CUSTOMER_TEL);
      console.log(that.data.IDCARD);
      
        wx.showToast({
          title: '请完善报名信息',
          icon: 'none',
          duration: 2000
        })
      } else {

        wx.request({
          url: app.globalData.interface+'/crossVersionOne/API2_2207_UpdateWeiXinUser',
          method: 'POST',
          data: {
            CUSTOMER_NAME: that.data.CUSTOMER_NAME,
            CUSTOMER_TEL: that.data.CUSTOMER_TEL,
            IDCARD: that.data.IDCARD,
            OPEN_ID: app.globalData.openid, //用户OPEN_ID
            session_key: app.globalData.session_key,
            OPEN_UUID: app.globalData.jiami,
            code: that.data.verify,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {

            if (res.data.result == 'error') {

              wx.showModal({
                title: '温馨提示',
                content: res.data.data,
                success: function (res) {
                  if (res.confirm) { //这里是点击了确定以后
               
                  } else { //这里是点击了取消以后
                    console.log('用户点击取消')
                  }
                }
              })
        
            } else {
              wx.showModal({
                title: '温馨提示',
                content: res.data.data,
                success: function (res) {
                  if (res.confirm) { //这里是点击了确定以后
               
                  } else { //这里是点击了取消以后
                    console.log('用户点击取消')
                  }
                }
              })
            }
          }
        })
      }
      setTimeout(() => {
        that.data.flag = true
      }, 3000)
    }
  },


















  // 复选框
  fuxk(event) {
    console.log(event.detail);
    this.setData({
      checked: event.detail,
    });
  },

  fuwu() {
    // this.setData({ show: true });
    this.setData({
      fuwu: !this.data.fuwu
    })
  },

  yinsi() {
    // this.setData({ show: true });
    this.setData({
      yinsi: !this.data.yinsi
    })
  },


  back() {
    wx.navigateBack({
      delta: 1
    })
  },







  //验证手机号
  mobileInput(e) {
    let value = e.detail.value.replace(/\D/g, '')
    this.setData({
      mobile: value,
    })
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(19[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (this.data.mobile.length == 0) {
      wx.showToast({
        title: '输入的手机号为空，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        CUSTOMER_TEL: ''
      })
    } else if (this.data.mobile.length < 11) {
      wx.showToast({
        title: '手机号长度有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        CUSTOMER_TEL: ''
      })

    } else if (!myreg.test(this.data.mobile)) {
      wx.showToast({
        title: '手机号有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        CUSTOMER_TEL: ''
      })

    } else {
      this.setData({
        mobileFormat: true,
      })
    }
  },


  idcardblur: function (e) {
    let idcard = e.detail.value;
    let reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;

    if (reg.test(idcard) == false) {
      this.setData({
        IDCARD: ''
      })
      wx.showToast({
        title: '身份证号码有误',
        icon: 'none',
        duration: 3000
      })



    } else {
      this.setData({
        IDCARD: idcard
      })


    }

  },







  search() {

    this.setData({

      jiami: app.globalData.jiami,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
      CUSTOMER_ID: app.globalData.CUSTOMER_ID,

    })





    wx.request({
      url: app.globalData.interface+'/newVersion/API_2010_selectUserSome',
      method: 'POST',
      data: {
        CUSTOMER_ID: this.data.CUSTOMER_ID,
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data);
        // var shenfenjiami = res.data.data.IDCARD.substr(0, 6) + "********" + res.data.data.IDCARD.substr(14)


        this.setData({
          zongnone: res.data.data,
          CUSTOMER_NAME: res.data.data.CUSTOMER_NAME,
          CUSTOMER_TEL: res.data.data.CUSTOMER_TEL,
          // shenfenjiami: shenfenjiami,
          // IDCARD: res.data.data.IDCARD,
          IDCARDNULL: res.data.data.IDCARDNULL,
        })

      }
    })
  },





























  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({

      jiami: app.globalData.jiami,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
      CUSTOMER_ID: app.globalData.CUSTOMER_ID,

    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.search()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})